//
//  ActivityListController.m
//  easystudy
//
//  Created by kane on 15/6/3.
//  Copyright (c) 2015年 kane. All rights reserved.
//

#import "ActivityListController.h"
#import "ActivityCell.h"
#import "ArticleDetailController.h"
#import "MJRefresh.h"

@interface ActivityListController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate>
{
    IBOutlet UITableView *dataTable;
    NSMutableArray *dataList;
    MJRefreshHeaderView *header;
    EmptyView *emptyView;
}

@end

@implementation ActivityListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    dataList = [[NSMutableArray alloc] init];
    [self initRefreshUI];

     [header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initRefreshUI
{
    emptyView = [[EmptyView alloc] init];
    [self.view insertSubview:emptyView belowSubview:dataTable];
    
    header = [MJRefreshHeaderView header];
    header.scrollView = dataTable;
    header.delegate = self;
}

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    NSDictionary *param = @{@"pageNo":@"1",@"pageSize":@"200"};
    [WebService findCourseList:param WithBlock:^(id result, NSError *error) {
        if (!error) {
            [dataList removeAllObjects];
            [dataList addObjectsFromArray:result[@"data"][@"dataList"]];
            [dataTable reloadData];
            emptyView.hidden = dataList.count > 0;
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        [refreshView endRefreshing];
    }];
}

//- (void)reloadDataFromServer
//{
//    NSDictionary *param = @{@"pageNo":@"1",@"pageSize":@"5"};
//    [WebService findCourseList:param WithBlock:^(id result, NSError *error) {
//        if (!error) {
//            [dataList removeAllObjects];
//            [dataList addObjectsFromArray:result[@"data"][@"dataList"]];
//            [dataTable reloadData];
//        }else{
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//        }
//    }];
//}

#pragma mark - UITableView DataSource && Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 235;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"ActivityCell";
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"ActivityCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *item = dataList[indexPath.row];
    NSString *url = item[@"coverimg"] && [item[@"coverimg"] isKindOfClass:[NSString class]] ? item[@"coverimg"]  : @"";
    [cell.imgView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",FileURL,url]] placeholderImage:[UIImage imageNamed:@"defaultHospital"]];
    cell.subjectLabel.text = item[@"title"];
    cell.descLabel.text = item[@"content"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = dataList[indexPath.row];
    ArticleDetailController *detailVC = [[ArticleDetailController alloc] init];
    detailVC.dataInfo = item;
    detailVC.artID = item[@"artid"];
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
